import {
    reqGoodInfo,
    reqAddOrUpdateShopCart
} from '@/api'
import {
    getUUID
} from '@/utils/uuid'

const state = {
    goodInfo: {},
    // 游客临时身份
    uuid_token: getUUID()
}
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo
    }
}
const actions = {
    //  获取产品信息的actions
    async getGoodInfo({
        commit
    }, skuId) {
        let result = await reqGoodInfo(skuId);
        if (result.code == 200) {
            commit("GETGOODINFO", result.data)
        }
    },

    // 获取购物车信息
    async addOrUpdateShopCart({
        commit
    }, {
        skuId,
        skuNum
    }) {
        let result = await reqAddOrUpdateShopCart(
            skuId,
            skuNum
        );
        if (result.code == 200) {
            return console.log("成功");;
        } else {
            return Promise.reject(new Error('fail'))
        }

    }

}
const getters = {
    categoryView(state) {
        // 至少是个空对象
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    // 产品售卖属性的简化
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList || []
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}